package com.yypx.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ObjectUtil
{
	private static final Log LOG = LogFactory.getLog(ObjectUtil.class);

	public static Object copy(Object oldObj)
	{
		Object obj = null;
		try
		{
			// Write the object out to a byte array
			ByteArrayOutputStream bOut = new ByteArrayOutputStream();
			ObjectOutputStream oOut = new ObjectOutputStream(bOut);
			oOut.writeObject(oldObj);
			oOut.flush();
			oOut.close();

			// Retrieve an input stream from the byte array and read a copy of
			// the object back in
			ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
			ObjectInputStream oIn = new ObjectInputStream(bIn);
			obj = oIn.readObject();
			oIn.close();
		} catch (IOException e)
		{
			LOG.error(e);
		} catch (ClassNotFoundException e)
		{
			LOG.error(e);
		}
		return obj;
	}
}
